 // pagesSubPackage/report/report.js
 const {
   ReportApi
 } = require('../common/apis/report')
 const mixin_formItemType = require('../common/mixins/formItemType')
 const {
   WxValidate
 } = require('../utils/WxValidate')
 const mixin_dicKey = require('../common/mixins/dicKey')
 import {
   PageName
 } from '../config/completePageName.config'
 import {
   toCompletePage
 } from '../utils/util'
 const app = getApp()
 Page({
   behaviors: [mixin_formItemType, mixin_dicKey],
   /**
    * 页面的初始数据
    */
   data: {
     // 指派科室列表
     deptList: [{
       label: '多选',
       value: 'checkbox1'
     }, ],
     // 选中指派科室
     checkedDeptValue: [],
     //  时间显示隐藏
     datetimeVisible: false,
     // 时间选择组件回显值
     dateTimePickerValue: Date.now(),
     //  预计工时单位
     timeUnit: '小时',
     //  实际报修地址
     actualRepairPlace: '',
     //  预约时间
     appointment: '',
     //  期望完成时间
     completeTime: '',
     //  创建时间
     createTime: Date.now(),
     //  紧急程度
     degree: '',
     //  报修科室
     deptId: '',
     //  附件
     enclosure: [],
     //  资产名称
     equipment: '',
     //  资产位置
     equipmentAddress: '',
     //  资产位置id
     equipmentAddressId: '',
     //  故障描述
     faultRemark: '',
     //  故障分类
     faultType: '',
     //  知识库ID
     learningId: '',
     //  报修电话
     phone: '',
     //  报修地址
     place: '',
     //  影响范围
     rangeType: '',
     //  录音
     recording: null,
     //  预计工时
     reqTime: '',
     //  响应类型 1即刻 2预约
     responseType: '1',
     //  派单对象标识
     sendObjId: null,
     //  派单对象类型
     sendObjType: '',
     //  派单所属小组
     sendTeam: '',
     //  指派类型:1由我处理，2派单
     sendType: 2,
     //  工单来源
     source: '',
     //  报修人
     user: '',
     rules: {
       createTime: {
         required: true,
       },
       faultRemark: {
         required: true,
       },
       faultType: {
         required: true,
       },
       sendType: {
         required: false,
         // 用于出发自定义校验
         sendType: true,
       }
     },
     message: {
       createTime: {
         required: '请选择创建时间',
       },
       faultRemark: {
         required: '请输入故障描述',
       },
       faultType: {
         required: '请选择故障分类',
       },
       // sendType: {
       //   required: '请选择指派科室',
       // }
     }
   },
   onResponseType(event) {
     const {
       value
     } = event.detail;

     this.setData({
       responseType: value,
       appointment: ''
     });
   },
   validateFormData(callback) {
     const {
       actualRepairPlace,
       appointment,
       completeTime,
       createTime,
       degree,
       deptId,
       enclosure,
       equipment,
       equipmentAddress,
       equipmentAddressId,
       faultRemark,
       faultType,
       learningId,
       phone,
       place,
       rangeType,
       recording,
       reqTime,
       responseType,
       sendObjId,
       sendObjType,
       sendTeam,
       sendType,
       source,
       user,
     } = this.data
     const formData = {
       actualRepairPlace,
       appointment,
       completeTime,
       createTime,
       degree,
       deptId,
       enclosure,
       equipment,
       equipmentAddress,
       equipmentAddressId,
       faultRemark,
       faultType,
       learningId,
       phone,
       place,
       rangeType,
       recording,
       reqTime,
       responseType,
       sendObjId,
       sendObjType,
       sendTeam,
       sendType,
       source,
       user,
     }
     const validate = new WxValidate(this.data.rules, this.data.message)
     // 自定义验证规则
     validate.addMethod('sendType', (value, param) => {
       console.log('param', param);
       //  派单
       if (value === 2) {
         return Boolean(this.data.checkedDeptValue.length)
       }
       return true
     }, '请选择指派科室')

     // 传入表单数据，调用验证方法
     if (!validate.checkForm(formData)) {
       const error = validate.errorList[0]
       wx.showToast({
         title: error.msg,
         icon: 'error'
       })
       return false
     }
     callback(formData)
   },
   // 提交
   onFormConfirm() {


     this.validateFormData((formData) => {

       ReportApi.create(formData, ({
         code,
         msg
       }) => {
         if (code === 200) {
           if (this.data.sendType == 2) {
             wx.$_.delay(toCompletePage(PageName.report), 1000)
           } else { 
             wx.$_.delay(toCompletePage(PageName.reportByMe), 1000)
           }
         }
       })
     })

   },
   // 取消
   onFormCancel() {

   },
   // 由我处理
   changeByMe({
     detail
   }) {
     this.setData({
       sendType: detail.value,
       checkedDeptValue: []
     })

   },
   // 指派 科室选中
   changeDept({
     detail
   }) {
     this.setData({
       checkedDeptValue: detail.value
     })
   },
   // 选择预约时间
   onSelectAppointment() {
     this.setData({
       datetimeVisible: true
     })
   },
   // 时间确认按钮
   onConfirmTime({
     detail
   }) {
     this.setData({
       appointment: detail.value
     })
   },
   // 时间取消按钮
   onCancelTime() {
     this.setData({
       datetimeVisible: false
     })
   },
   //  期望完成时间单位选择
   onSelectTimeUint() {
     const units = ['小时', '分钟']
     const self = this
     wx.showActionSheet({
       itemList: units,
       success({
         tapIndex
       }) {
         self.setData({
           timeUnit: units[tapIndex]
         })
       }
     })
   },
   //  监听附件的值
   onUploadData({
     detail
   }) {
     const {
       value,
       type
     } = detail
     if (type === 'fileList') {
       this.setData({
         enclosure: value
       })
     } else {
       let {
         enclosure
       } = this.data
       enclosure.push(...value)
       this.setData({
         enclosure: enclosure
       })
     }

   },

   //  hook
   onReadyUserInfo(options) {
     //  this.initUserInfo()
     //  this.initOrderDeptList()
   },
   /**
    * 生命周期函数--监听页面加载
    */
   onLoad(options) {
     this.initOrderDeptList()
     this.initUserInfo()

   },


   //  初始化用户数据
   initUserInfo() {
     const {
       userInfo
     } = app.globalData
     this.setData({
       user: userInfo.userName,
       phone: userInfo.phone,
       deptId: userInfo.deptId,
     })
   },
   // 初始化默认故障分类
   initDefaultType() {

   },
   //  获取接单科室
   initOrderDeptList() {
     const self = this
     ReportApi.getOrderDeptList((res) => {
       self.setData({
         deptList: res.data.map((item) => {
           return {
             label: item.departName,
             value: item.departId
           }
         })
       })
     })
   },


   /**
    * 生命周期函数--监听页面初次渲染完成
    */
   onReady() {},

   /**
    * 生命周期函数--监听页面显示
    */
   onShow() {

   },

   /**
    * 生命周期函数--监听页面隐藏
    */
   onHide() {

   },

   /**
    * 生命周期函数--监听页面卸载
    */
   onUnload() {

   },

   /**
    * 页面相关事件处理函数--监听用户下拉动作
    */
   onPullDownRefresh() {

   },

   /**
    * 页面上拉触底事件的处理函数
    */
   onReachBottom() {

   },

   /**
    * 用户点击右上角分享
    */
   onShareAppMessage() {

   }
 })